// pages/binglogin/binglogin.js
const app = getApp();
var network = require("../../utils/net.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    userCode: '',
    pwd: '',
    noinput: false,
    pwdinput: false,
  },
  noinput: function(e) {
    this.setData({
      userCode: e.detail.value
    });
    this.setData({
      noinput: true
    });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({
        disabled: false
      });
    }

  },
  pwdinput: function(e) {
    this.setData({
      pwd: e.detail.value
    });
    this.setData({
      pwdinput: true
    });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({
        disabled: false
      });
    }
  },
  formSubmit: function(e) {
    wx.showLoading({
      title: '登录中...',
    })
    this.setData({
      disabled: true
    });
    var openId = wx.getStorageSync(app.userKey).openid;
    network.POST({
      params: { 
        openId: openId,
        userCode: e.detail.value.userCode,
        password: e.detail.value.pwd
      },
      API_URL: "/wechat/bindUserMation",
      success: (res) => {
        this.setData({
          disabled: false
        });
        if (res.returnCode == 0) {
          if(res.bean && res.bean.userId){
            wx.setStorageSync(app.consoleUserKey, res.bean); //存储openid
          }else{
            wx.showToast({
              mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
              title: '账号绑定失败',
              icon: 'none',
            });
          }
        } else {
          wx.showToast({
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
            title: res.returnMessage,
            icon: 'none',
          });
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '服务器出现错误',
          icon: 'none',
          duration: 2000
        });
      }
    })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.userCode == '' || this.data.pwd == '') {
      this.setData({
        disabled: true
      });
    } else {
      this.setData({
        disabled: false
      });
    }
  },

})